package com.example.recuitactivity;


import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.bean.Enterprise;
import com.example.recuitactivity.helper.DbOpenHelper;

public class EnterpriseFragment extends Fragment {
    private View messageLayout;
    private EditText et_username;
    private EditText et_passwrod;
    private EditText et_company;
    private TextView tv_username_check;
    private TextView tv_password_check;
    private TextView tv_company_check;
    private RadioGroup rg_scale;
    private Button btn_register;
    private DbOpenHelper dbOpenHelper;

    String scale="小型";

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.btn_register_enterprise:
                    register();
                    break;
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener listener2=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(i==R.id.rb_scale1){
                scale="小型";
            }else if(i==R.id.rb_scale2){
                scale="中型";
            }else if(i==R.id.rb_scale3){
                scale="大型";
            }else{
                scale="特大型";
            }
        }
    };

    public void initUI() {
        et_username=(EditText) messageLayout.findViewById(R.id.et_username_register_enterprise);
        et_passwrod=(EditText) messageLayout.findViewById(R.id.et_password_register_enterprise);
        et_company=(EditText) messageLayout.findViewById(R.id.et_name_register_enterprise);
        tv_username_check=(TextView) messageLayout.findViewById(R.id.tv_username_register_enterprise_check);
        tv_password_check=(TextView) messageLayout.findViewById(R.id.tv_pwd_register_enterprise_check);
        tv_company_check=(TextView) messageLayout.findViewById(R.id.tv_name_register_enterprise_check);
        rg_scale=(RadioGroup) messageLayout.findViewById(R.id.rg_scale);
        btn_register=(Button) messageLayout.findViewById(R.id.btn_register_enterprise);

        btn_register.setOnClickListener(listener);
        rg_scale.setOnCheckedChangeListener(listener2);
    }

    public void register(){
        dbOpenHelper=new DbOpenHelper(getActivity());

        String username = et_username.getText().toString();
        String password = et_passwrod.getText().toString();
        String company=et_company.getText().toString();
        boolean register_successs = true;

        tv_username_check.setText("");
        tv_password_check.setText("");
        tv_company_check.setText("");

        if (username == null || username.length() == 0) {
            register_successs = false;
            tv_username_check.setText("用户名不能为空！");
        }else if (dbOpenHelper.isExistEnterpriseByusername(username)) {//用户名重复
            register_successs = false;
            tv_username_check.setText("该用户名已被注册！");
        }
        if(company==null||company.length()==0){
            register_successs = false;
            tv_company_check.setText("企业名不能为空");
        }else if (dbOpenHelper.isExistEnterpriseBycompany(company)) {//用户名重复
            register_successs = false;
            tv_company_check.setText("该企业已被注册！");
        }
        if (password==null||password.length() ==0) {
            register_successs = false;
            tv_password_check.setText("密码不能为空！");
        }
        if (register_successs) {
            Enterprise enterprise = new Enterprise(username,password,company,scale);
            dbOpenHelper.insertEnterprise(enterprise);
            Toast.makeText(getActivity(),"恭喜你，注册成功！",Toast.LENGTH_LONG).show();
            getActivity().finish();
        } else {
            Toast.makeText(getActivity(), "注册失败，请检查注册信息！", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.enterprise_fragment, container, false);
        initUI();
        return messageLayout;
    }
}